<?php
namespace Common\Model;
use Think\Model;

/**
 * Class WeatherchangeModel
 * @package Common\Model
 * 天气
 */
class WeatherchangeLogModel extends BaseModel{
    public $pix = "";
    public $fieldsStr = '*';
    public $orderStr = 'id desc';

    public function get_list_use_page($map=array(), $cur_page, $rows_num){
        $map = filter_query_map($map);
        $map['facilities_type'][0] = 'eq';
        $map['facilities_type'][1] = $facilities_type = str_replace('%', '', $map['facilities_type'][1]);
        $map['facilities_id'][0] = 'eq';
        $map['facilities_id'][1] = str_replace('%', '', $map['facilities_id'][1]);
        $facilities_ids = explode(',', $map['facilities_id'][1]);
//        $map = array();
        $map['facilities_type'] = array('eq', $facilities_type);
        $where_str = '';
        for($i=0;$i<count($facilities_ids);$i++){
            $where_str .= 'facilities_id='.$facilities_ids[$i];
            if($i < count($facilities_ids)-1){
                $where_str .= ' or ';
            }
        }
        unset($map['facilities_id']);
        if(isset($map['rain_sit'])){
            $map['rain_sit'][0] = 'eq';
            $map['rain_sit'][1] = str_replace('%', '', $map['rain_sit'][1]);
        }
        if($map['start']&& $map['end']){
            $map['start'][1] = str_replace('%', '', $map['start'][1]);
            $map['end'][1] = str_replace('%', '', $map['end'][1]);
            $date_str = '(date>=\''.$map['start'][1].' 00:00:00'.'\' and date<=\''.$map['end'][1].' 23:59:59'.'\') ';
            if($where_str)
                $where_str = '('.$where_str.') and '.$date_str;
            unset($map['start']);
            unset($map['end']);
        }
//        dump($where_str);
//        dump($map);
        if($where_str){
            $count = $this->where($map)->where($where_str)->count();
            $list = $this->where($map)->where($where_str)->order($this->orderStr)->page($cur_page.",$rows_num")->select();
        }else if(!$where_str&&!empty($map)){
            $count = $this->where($map)->count();
            $list = $this->where($map)->order($this->orderStr)->page($cur_page.",$rows_num")->select();
        }else{
            $count = 0;
            $list = array();
        }
        foreach($list as $k => $v){
            switch($facilities_type){
                case 1:
                    $station_model = D('PumpingStation');
                    $sta_temp = $station_model->queryObj($v['facilities_id'], 'id');
                    $list[$k]['sta_name'] = $sta_temp['ps_name'];
                    break;
                case 2:
                    $station_model = D('Csdt');
                    $sta_temp = $station_model->queryObj($v['facilities_id'], 'id');
                    $list[$k]['sta_name'] = $sta_temp['ps_name'];
                    break;
                case 3:
                    $station_model = D('Gate');
                    $sta_temp = $station_model->queryObj($v['facilities_id'], 'id');
                    $list[$k]['sta_name'] = $sta_temp['gate_name'];
                    break;
            }
            $list[$k]['facilities_type'] = gate_facilities_type_option($list[$k]['facilities_type'], false);
            $list[$k]['rain_sit'] = weather_status_option($list[$k]['rain_sit'], false);
        }
        //如果泵站没有泵机，则他就没有记录

        return array('total'=>$count, 'list'=>$list);
    }

    public function get_live_rain_report($map, $page, $rows_num){
        //查询出所有泵站，调蓄池，河道闸门当前正在下雨的记录
        $station_model = D('PumpingStation');
        $csdt_model = D('Csdt');
        $gate_model = D('Gate');
        $all_array = array();
        $temp_array = array();
        $small_rain = array();
        $middle_rain = array();
        $big_rain = array();
        //查询设施
        if(isset($map['name'])){
            $station_map['ps_name'] = $map['name'];
            $csdt_map['ps_name'] = $map['name'];
            $gate_map['gate_name'] = $map['name'];
            unset($map['name']);
            $stations = $station_model->where($station_map)->select();
            $csdts = $csdt_model->where($csdt_map)->select();
            $gates = $gate_model->where($gate_map)->select();
        }else{
            $stations = $station_model->select();
            $csdts = $csdt_model->select();
            $gates = $gate_model->select();
        }
        if(isset($map['rain_sit'])){
            $map['rain_sit'][0] = 'eq';
            $map['rain_sit'][1] = str_replace('%', '', $map['rain_sit'][1]);
            $rain_sit = $map['rain_sit'][1];
            unset($map['rain_sit']);
        }
        //根据设施查询最新雨情
        foreach($stations as $station){
            $map['facilities_type'] = array('eq', 1);
            $map['facilities_id'] = array('eq', $station['id']);
            $one = $this->where($map)->order('date desc')->find();
            if(isset($rain_sit)&&$rain_sit != $one['rain_sit']){
                continue;
            }
            if($one['rain_sit'] == 1 || !$one['rain_sit'])
                continue;
            //计算持续时间
            $one['name'] = $station['ps_name'];
            $one['realname'] = $one['dutyperson'];
            $one['mobile'] = $station['mobile'];
            $one['phone'] = $station['phone'];
            $one['rain_sit_text'] = weather_status_option($one['rain_sit'], false);
            $one['stil_time'] = (time()-strtotime($one['date']))/(60*60);
            $k = $one['stil_time'];
            $one['stil_time'] = round($one['stil_time'], 2);
            if($one['rain_sit']==2)
                $small_rain[$k.''] = $one;
            else if($one['rain_sit'] == 3)
                $middle_rain[$k.''] = $one;
            else if($one['rain_sit']==4)
                $big_rain[$k.''] = $one;

        }
        foreach($csdts as $csdt){
            $map['facilities_type'] = array('eq', 2);
            $map['facilities_id'] = array('eq', $csdt['id']);
            $one = $this->where($map)->order('date desc')->find();
            if(isset($rain_sit)&&$rain_sit != $one['rain_sit']){
                continue;
            }
            if($one['rain_sit'] == 1 || !$one['rain_sit'])
                continue;
            //计算持续时间
            $one['name'] = $csdt['ps_name'];
            $one['realname'] = $one['dutyperson'];
            $one['mobile'] = $csdt['mobile'];
            $one['phone'] = $csdt['phone'];
            $one['rain_sit_text'] = weather_status_option($one['rain_sit'], false);
            $one['stil_time'] = (time()-strtotime($one['date']))/(60*60);
            $k = $one['stil_time'];
            $one['stil_time'] = round($one['stil_time'], 2);
            if($one['rain_sit']==2)
                $small_rain[$k.''] = $one;
            else if($one['rain_sit'] == 3)
                $middle_rain[$k.''] = $one;
            else if($one['rain_sit']==4)
                $big_rain[$k.''] = $one;
        }
        foreach($gates as $gate){
            $map['facilities_type'] = array('eq', 3);
            $map['facilities_id'] = array('eq', $gate['id']);
            $one = $this->where($map)->order('date desc')->find();
            if(isset($rain_sit)&&$rain_sit != $one['rain_sit']){
                continue;
            }
            if($one['rain_sit'] == 1 || !$one['rain_sit'])
                continue;
            //计算持续时间
            $one['name'] = $gate['gate_name'];
            $one['realname'] = $one['dutyperson'];
            $one['mobile'] = $gate['mobile'];
            $one['phone'] = $gate['phone'];
            $one['rain_sit_text'] = weather_status_option($one['rain_sit'], false);
            $one['stil_time'] = (time()-strtotime($one['date']))/(60*60);
            $k = $one['stil_time'];
            $one['stil_time'] = round($one['stil_time'], 2);
            if($one['rain_sit']==2)
                $small_rain[$k.''] = $one;
            else if($one['rain_sit'] == 3)
                $middle_rain[$k.''] = $one;
            else if($one['rain_sit']==4)
                $big_rain[$k.''] = $one;
        }
        //先按照大中小雨区分，数组结构：持续时间=>记录
        krsort($big_rain);
        krsort($middle_rain);
        krsort($small_rain);
        $all_array = array_merge(array_values($big_rain), array_values($middle_rain), array_values($small_rain));
        $list = array_slice($all_array, ($page-1)*$rows_num, $rows_num);
        return array('total'=>count($all_array), 'rows'=>$list);
    }

    public function get_last_one($facilities_type, $facilities_id){
        $map['facilities_type'] = array('eq', $facilities_type);
        $map['facilities_id'] = array('eq', $facilities_id);
        $one = $this->where($map)->order('date desc')->find();
        return $one?$one['rain_sit']:999;
    }

    public function get_last_one_record($facilities_type, $facilities_id){
        $map['facilities_type'] = array('eq', $facilities_type);
        $map['facilities_id'] = array('eq', $facilities_id);
        $one = $this->where($map)->order('date desc')->find();
        return $one?$one:false;
    }



    public function addObj($post, $f_title='id'){
        //判重
        $data[$f_title] = array("eq", $post[$f_title]);
        $one = $this->where($data)->find();
        $post['date'] = getNowTime();
        switch($post['status']){
            case 0:
                break;
            case 1:
                break;
        }
        if(!$one){
            //如果没有记录，就添加
            return $this->add($post);
        }else{
            //反之更新
            return $this->save($post);
        }
    }
}